package com.gaattc.controller.mall;

import com.gaattc.model.Result;
import com.gaattc.model.vo.GetGoodsByTypeVO;
import com.gaattc.model.vo.GoodsTypeVO;
import com.gaattc.service.GoodsService;
import com.gaattc.service.GoodsServiceImpl;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/api/mall/index/*")
public class IndexServlet extends HttpServlet {

    private GoodsService goodsService = new GoodsServiceImpl();

    private Gson gson = new Gson();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/mall/index/", "");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/mall/index/", "");
        if("getType".equals(action)) {
            getType(request, response);
        }
    }

    /**
     * 获取类型信息
     * @param request req
     * @param response resp
     * @throws IOException io
     */
    private void getType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<GoodsTypeVO> list = goodsService.getType();
        response.getWriter().println(gson.toJson(Result.ok(list)));
    }
}
